﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Submitter
{
    class AutoSelectAllTextEdit : DevExpress.XtraEditors.TextEdit
    {
        private bool alreadyFocused;

        public AutoSelectAllTextEdit()
        {
            this.GotFocus += OnGotFocus;
            this.Leave += OnLeave;
            this.MouseUp += OnMouseUp;
        }

        private void OnMouseUp(object sender, MouseEventArgs mouseEventArgs)
        {
            if (!alreadyFocused && this.SelectionLength == 0)
            {
                alreadyFocused = true;
                this.SelectAll();
            }
        }

        private void OnLeave(object sender, EventArgs eventArgs)
        {
            alreadyFocused = false;
        }

        private void OnGotFocus(object sender, EventArgs eventArgs)
        {
            if (MouseButtons == MouseButtons.None)
            {
                this.SelectAll();
                alreadyFocused = true;
            }
        }
    }
}
